<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/17
 * Time: 11:24
 */

namespace app\controllers;

use app\models\Invoice;
use yii;
use app\models\Pi;

class PiController extends ApiBaseController
{
    /**Pi列表
     * @return string
     */
    public function actionList()
    {
        $page = yii::$app->request->get('page', 1);
        $pageSize = yii::$app->request->get('pageSize', 10);
        $where = [];
        $where['pi_id'] = yii::$app->request->get('pi_id', '');
        $where['job_number'] = yii::$app->request->get('staff', '');
        $where['number'] = yii::$app->request->get('number', '');
        $where['customer_name'] = yii::$app->request->get('customer_name', '');
        if ($this->user['role'] != 1 && $this->user['role'] != 2) {
            $where['job_number'] = $this->user['job_number'];
        }
        $model = new Pi();
        $list = $model->list('*', $where, $page, $pageSize);
        foreach ($list as $k => $v) {
            $list[$k]['product_list'] = \json_decode($v['product_list'], true);
            $list[$k]['customer_info'] = \json_decode($v['customer_info'], true);
        }
        $total = $model->size($where);
        //历史记录
        $piArr = array_column($list, 'pi_id');
        if ($piArr) {
            $where['pi_id'] = $piArr;
            $oldList = $model->get_all_older_pi($where);
        } else {
            $oldList = [];
        }
        $piIdArr = array_column($list, 'id');
        //获取IN信息
        if ($piIdArr) {
            $model = new Invoice();
            $inList = $model->get_invoice_by_pi(['pi_id' => $piIdArr]);
        } else {
            $inList = [];
        }
        return \json_encode(['in' => $inList, 'list' => $list, 'oldList' => $oldList, 'total' => (int)$total]);
    }

    public function actionInfo()
    {
        $id = yii::$app->request->get('id');
        if (!is_numeric($id) || $id < 0) {
            return \json_encode(['code' => 0, 'msg' => '数据格式不正确']);
        }
        $db = new Pi();
        $info = $db->one('*', ['id' => $id]);
        return \json_encode($info);
    }

    /**
     * 创建Pi
     */
    public function actionAdd()
    {
        $data = yii::$app->request->post('data');
        try {
            $msg = \json_decode($data, true);
        } catch (\Exception $e) {
            return \json_encode(['code' => 0, 'msg' => '数据错误']);
        }

        $id = $msg['quote_id'];
        if (!is_numeric($id) || $id < 0) {
            return \json_encode(['code' => 0, 'msg' => '数据错误2']);
        }
        $model = new \app\models\Quote();
        $data = $model->get_quote_info_by_id($id);
        $data['customer_info'] = \json_encode($msg['customer_info']);
        $data['para'] = \json_encode($msg['para']);//段落
        $data['duty'] = \json_encode($msg['duty']);//税费
        $data['ship'] = \json_encode($msg['ship']);//运费
        $data['service'] = \json_encode($msg['service']);//运费
        $data['product_list'] = \json_encode($msg['list']);
        $data['pay_show'] = \json_encode($msg['pay_show']);
        $data['create_at'] = time();
        $data['version'] = 1;
        $PiModal = new Pi();
        $number = $PiModal->get_pi_number();
        $data['pi_id'] = $number;
        $data['quote_id'] = $data['id'];
        unset($data['id']);
        $re = $PiModal->add_pi($data);
        $data = $re ? ['code' => 1, 'msg' => '添加成功'] : ['code' => 0, 'msg' => '添加失败,请重试'];
        return \json_encode($data);
    }

    public function actionUpdate()
    {
        $data = yii::$app->request->post('data');
        $id = yii::$app->request->post('id');
        if (!is_numeric($id) || $id < 0) {
            return \json_encode(['code' => 0, 'msg' => '参数错误']);
        }
        $model = new Pi();
        $info = $model->one('*', ['id' => $id]);
        try {
            $data = json_decode($data, true);
            $info['customer_info'] = \json_encode($data['customer_info']);
            $info['para'] = \json_encode($data['para']);//段落
            $info['duty'] = \json_encode($data['duty']);//税费
            $info['ship'] = \json_encode($data['ship']);//运费
            $info['service'] = \json_encode($data['service']);//运费
            $info['product_list'] = \json_encode($data['list']);
            $info['pay_show'] = \json_encode($data['pay_show']);
            $info['create_at'] = time();
            //获取最大版本信息
            $num = (int)$model->get_max_pi_version($info['pi_id']);
            $num++;
            $info['version'] = $num;
            unset($info['id']);
            $re = $model->add_pi($info);
            $output = $re ? ['code' => 1, 'msg' => '更新成功'] : ['code' => 0, 'msg' => '更新失败'];
            return \json_encode($output);
        } catch (\Exception $e) {
            return \json_encode(['code' => 0, 'msg' => $e->getMessage()]);
        }
    }
}